package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityStressMonitorBinding;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.SwitchDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.StressMonitorCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleAutoStressMonitor;
import com.cyber.bridge.module.bean.StressSetEntity;
import com.cyber.bridge.module.listener.OnCyberStressConfigListener;

public class StressMonitorActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    SwitchDialog switchDialog;
    int intervalTime;
    MonthDialog monthDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                StressSetEntity stressSetEntity = (StressSetEntity) msg.obj;
                if (stressSetEntity == null) {
                    return;
                }
                if (stressSetEntity.isEnabled()) {
                    activityStressMonitorBinding.restScreen.setText(getString(R.string.switch_state_on));
                } else {
                    activityStressMonitorBinding.restScreen.setText(getString(R.string.switch_state_close));
                }
                intervalTime = stressSetEntity.getValue();
                activityStressMonitorBinding.intervalTime.setText(intervalTime + "");
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(StressMonitorActivity.this, getString(R.string.add_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityStressMonitorBinding activityStressMonitorBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityStressMonitorBinding = ActivityStressMonitorBinding.inflate(getLayoutInflater());
        setContentView(activityStressMonitorBinding.getRoot());
        activityStressMonitorBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityStressMonitorBinding.tool);
        getSupportActionBar().setTitle(null);
        activityStressMonitorBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnStressConfigListener(new OnCyberStressConfigListener() {
            @Override
            public void queryStressAutoMeasure(@Nullable StressSetEntity stressSetEntity) {
                if (mHandler != null) {
                    Message message = Message.obtain();
                    message.what = 0x40;
                    message.obj = stressSetEntity;
                    mHandler.sendMessage(message);
                }

            }

            @Override
            public void setStressAutoMeasure(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(StressMonitorActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryStressAutoMeasure();
        activityStressMonitorBinding.restScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchDialog == null) {
                    switchDialog = new SwitchDialog(StressMonitorActivity.this);
                    switchDialog.setSelectListener(new SwitchDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityStressMonitorBinding.restScreen.setText(sex);
                            boolean open = false;
                            if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                open = true;
                            } else {
                                open = false;
                            }
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(StressMonitorActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setStressAutoMeasure(new StressSetEntity(open, intervalTime));

                        }
                    });
                    if (!switchDialog.isShowing()) {
                        switchDialog.show();
                    }
                }
            }
        });
        activityStressMonitorBinding.intervalTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monthDialog == null) {
                    monthDialog = new MonthDialog(StressMonitorActivity.this, getString(R.string.interval_time));
                    monthDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            intervalTime = sex;
                            activityStressMonitorBinding.intervalTime.setText(intervalTime + "");
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(StressMonitorActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setStressAutoMeasure(new StressSetEntity(getString(R.string.switch_state_on).equalsIgnoreCase(activityStressMonitorBinding.restScreen.getText().toString()) ? true : false, intervalTime));
                        }
                    });
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        if (switchDialog != null) {
            switchDialog.destroyDialog();
            switchDialog = null;
        }
        super.onDestroy();
    }

}
